package com.bops.biz.permission;

import java.util.ArrayList;
import java.util.List;

public class Role {

    private boolean      enabled;
    private String       name;
    private String       id;
    private String       group;
    private List<String> grantList;
    private List<String> menuList; //访问的菜单权限
    private List<Role> roleList;
    
    public String getGroup() {
        return group;
    }

    public void setGroup(String group) {
        this.group = group;
    }

    /**
     * 判断是否有权限
     */
    public boolean hasPermission(String roleId)
    {
        if (roleId==null)
        {
            return false;
        }
        if (roleId.equalsIgnoreCase(id))
        {   
            return true;
        }
        if (roleList==null)
        {
            return false;
        }
        for (Role tmp:roleList)
        {
            if (tmp!=null && tmp.hasPermission(roleId))
            {
                return true;
            }
        }
        return false;
    }
    
    public List<String> getMenuList() {
        return menuList;
    }

    public void setMenuList(List<String> menuList) {
        this.menuList = menuList;
    }


    public List<Role> getRoleList() {
        return roleList;
    }

    public void setRoleList(List<Role> roleList) {
        this.roleList = roleList;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<String> getGrantList() {
        return grantList;
    }

    public void setGrantList(List<String> grantList) {
        this.grantList = grantList;
    }
    /**
     * 返回所有的Grant,包括parent的Role
     * @return
     */
    public List<String> getAllGrangList()
    {
        List<String> tmpList = new ArrayList<String>();
        if (grantList!=null)
        {
            tmpList.addAll(grantList);
        }
        if (roleList!=null)
        {
            for (Role son:roleList)
            {
                List<String> sonTmpList = son.getAllGrangList();
                if (sonTmpList!=null && sonTmpList.size()>0)
                {
                    for (String sonTmp:sonTmpList)
                    {
                        if (sonTmp.trim().length()>0 && !tmpList.contains(sonTmp))
                        {
                            tmpList.add(sonTmp);
                        }
                    }
                }
            }
        }
        return tmpList;
    }
    /**
     * 返回所有的菜单权限,包括parent的菜单权限
     * @return
     */
    public List<String> getAllMenuList()
    {
        List<String> tmpList = new ArrayList<String>();
        if (menuList!=null)
        {
            tmpList.addAll(menuList);
        }
        if (roleList!=null)
        {
            for (Role son:roleList)
            {
                List<String> sonTmpList = son.getAllMenuList();
                if (sonTmpList!=null && sonTmpList.size()>0)
                {
                    for (String sonTmp:sonTmpList)
                    {
                        if (sonTmp.trim().length()>0 && !tmpList.contains(sonTmp))
                        {
                            tmpList.add(sonTmp);
                        }
                    }
                }
            }
        }
        return tmpList;
    }
    
    
    /**
     * 检查角色是否存在死循环
     * @return
     */
    public boolean check(String parentID) throws RuntimeException
    {
        if (roleList!=null)
        {
            for (Role son:roleList)
            {
                String sonId = son.getId();
                if (parentID.equals(sonId))
                {
                    throw new RuntimeException("错误! parentID="+parentID+" son="+getId()+" is error! 出现死循环，请检查!");
                }
                if (!son.check(parentID))
                {
                    return false;
                }
            }
        }
        return true;
    }
}
